package com.azxcrm.project.system.wap.controller;

import java.util.List;
import java.util.UUID;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.azxcrm.common.utils.DateUtils;
import com.azxcrm.common.utils.RandomUtils;
import com.azxcrm.common.utils.StringUtils;
import com.azxcrm.common.utils.security.ShiroUtils;
import com.azxcrm.framework.aspectj.lang.annotation.Log;
import com.azxcrm.framework.web.domain.Message;
import com.azxcrm.project.monitor.workflow.common.CommonService;
import com.azxcrm.project.monitor.workflow.domain.WorkFlowApply;
import com.azxcrm.project.monitor.workflow.service.WorkFlowApplyService;
import com.azxcrm.project.system.basic.domain.OrderModifyRecord;
import com.azxcrm.project.system.basic.service.IOrderModifyRecordService;
import com.azxcrm.project.system.customer.domain.CustomerAttendance;
import com.azxcrm.project.system.customer.domain.CustomerClue;
import com.azxcrm.project.system.customer.service.ICustomerAttendanceService;
import com.azxcrm.project.system.customer.service.ICustomerClueService;
import com.azxcrm.project.system.dict.domain.DictData;
import com.azxcrm.project.system.dict.service.IDictDataService;
import com.azxcrm.project.system.order.domain.Order;
import com.azxcrm.project.system.order.service.IOrderService;
import com.azxcrm.project.system.product.domain.ProductInfo;
import com.azxcrm.project.system.product.domain.ProductSpecification;
import com.azxcrm.project.system.product.service.IProductInfoService;
import com.azxcrm.project.system.product.service.IProductSpecificationService;
import com.azxcrm.project.system.wap.domain.CustomerVisitRecord;
import com.azxcrm.project.system.wap.service.ICustomerVisitRecordService;

@Controller
@RequestMapping("/wap/customer")
public class CustomerWapController {

	private String prefix = "wap/customer";
	//客户线索
	@Autowired
	private ICustomerClueService customerClueService;
	//参课记录
	@Autowired
	private ICustomerAttendanceService customerAttendanceService;
	//线索客户拜访
	@Autowired
	private ICustomerVisitRecordService customerVisitRecordService;
	//字典
	@Autowired
	private IDictDataService dictDataService;
	//订单记录
	@Autowired
	private IOrderService orderService;
	//产品品种
	@Autowired
	private IProductSpecificationService productSpecificationService;
	//产品类型
	@Autowired
	private IProductInfoService productInfoService;
	//工作流
	@Autowired
	private WorkFlowApplyService workFlowApplyService;
	@Autowired
	private CommonService commonService;
	/**
	 * 订单操作记录
	 */
	@Autowired
	private IOrderModifyRecordService orderModifyRecordService;
	
	/**
	 * 拜访类型
	 */
	@Value("${crm.dictionary.visit.type}")
	private String visitType;
	
	/**
	 * 字典产品类型
     */
    @Value("${crm.dictionary.product.type.orign}")
    private String productType;
    
    /**
     * 客户新增
	 */
	@Log(title = "客户新增", action = "wap端新增客户页面")
	// 客户新增
	@GetMapping("/addCustomer")
	@RequiresPermissions("module:customerClue:offLineAdd")
	public String wapAddCustomer(Model model) {
		model.addAttribute("customerClue", new CustomerClue());
		return prefix + "/addCustomer";
	}
	
	/**
     * 客户详情
	 */
	@Log(title = "客户详情", action = "wap端客户详情页面")
	@GetMapping("/customerInfo")
	public String wapCustomer(@RequestParam("clueId") Integer clueId,@RequestParam("status")Integer status, Model model) {
		//线索客户详情
		CustomerClue clue = customerClueService.selectCustomerClueById(clueId);
		model.addAttribute("clue", clue);
		//参课记录
		CustomerAttendance customerAttendance = new CustomerAttendance();
		customerAttendance.setSalesUserId(ShiroUtils.getUserId().intValue());
		customerAttendance.setCellphoneNum(clue.getCustomerCellphone());
		CustomerAttendance dance = customerAttendanceService.selectCustomerAttendanceNewest(customerAttendance);
		model.addAttribute("dance", dance);
		//线索客户拜访
		CustomerVisitRecord customerVisitRecord = new CustomerVisitRecord();
		customerVisitRecord.setClueId(clueId);
		List<CustomerVisitRecord> record = customerVisitRecordService.selectCustomerVisitRecordList(customerVisitRecord);
		model.addAttribute("record", record);
		//订单记录
		Order order = new Order();
		order.setClueId(clueId);
		order.setOrderType(1);
		List<Order> er = orderService.selectOrderList(order);
		model.addAttribute("er", er);

		model.addAttribute("status", status);
		return prefix + "/customerInfo";
	}
	
	/**
     * 客户详情编辑修改
	 */
	@Log(title = "客户详情编辑修改", action = "wap端客户详情编辑修改")
	@GetMapping("/editCustomer")
	public String editCustomer(@RequestParam("clueId") Integer clueId,@RequestParam("orderId")Integer orderId,@RequestParam("status")Integer status, Model model) {
		//线索客户详情
		CustomerClue clue = customerClueService.selectCustomerClueById(clueId);
		model.addAttribute("clue", clue);
		model.addAttribute("orderId", orderId);
		model.addAttribute("status", status);
		return prefix + "/editCustomer";
	}
	
	/**
     * 保存客户信息
	 */
	@Log(title = "保存客户信息", action = "wap端保存客户信息")
	@PostMapping("/saveCustomer")
	@ResponseBody
	public Message saveCustomer(CustomerClue customerClue) {
		//新增客户线索客户
		if (customerClueService.saveCustomerClue(customerClue)>0) {
			return Message.ok();
		}
		return Message.error();
	}
	
	/**
     * AJAX获取拜访类型
	 */
	@GetMapping("/getVisit")
	@ResponseBody
	public List<DictData> getVisit() {
		List<DictData> visitList = dictDataService.selectDictDataByDictType(visitType);
		return visitList;
	}
	
	/**
     * 添加拜访记录
	 */
	@Log(title = "添加拜访记录", action = "wap端添加拜访记录")
	@GetMapping("/addRecord")
	public String addRecord(@RequestParam("clueId") Integer clueId,@RequestParam("status")Integer status, Model model) {
		model.addAttribute("clueId", clueId);
		model.addAttribute("status", status);
		return prefix + "/addRecord";
	}
	
	/**
     * 保存拜访记录
	 */
	@Log(title = "保存拜访记录", action = "wap端保存拜访记录")
	@PostMapping("/saveRecord")
	@ResponseBody
	public Message saveRecord(CustomerVisitRecord customerVisitRecord) {
		//新增客户线索客户
		if (customerVisitRecordService.saveCustomerVisitRecord(customerVisitRecord)>0) {
			return Message.ok();
		}
		return Message.error();
	}
	
	/**
     * 创建订单
	 */
	@Log(title = "创建订单", action = "wap端创建订单")
	@GetMapping("/addOrder")
	@RequiresPermissions("module:customerClue:offLineAdd")
	public String addOrder(@RequestParam("clueId") Integer clueId,@RequestParam("status")Integer status, Model model) {
		//线索客户详情
		CustomerClue clue = customerClueService.selectCustomerClueById(clueId);
		model.addAttribute("clue", clue);
		model.addAttribute("status", status);
		return prefix + "/addOrder";
	}
	
	/**
     * 创建订单页面AjAX获取订单类型
	 */
	@GetMapping("/getOrderType")
	@ResponseBody
	public List<DictData> getOrderType() {
		//订单类型类型
		List<DictData> productList = dictDataService.selectDictDataByDictType(productType);
		return productList;
	}
	
	/**
     * 创建订单页面AjAX获取产品品种
	 */
	@GetMapping("/getProductSpecification")
	@ResponseBody
	public List<ProductSpecification> getProductSpecification(ProductSpecification productSpecification) {
		//商品类型
		List<ProductSpecification> specification = productSpecificationService.selectProductSpecificationList(productSpecification);
		return specification;
	}
	
	/**
     * 创建订单页面AjAX获取产品类型
	 */
	@GetMapping("/getProductInfo")
	@ResponseBody
	public List<ProductInfo> getProductInfo(ProductInfo productInfo) {
		//商品类型
		List<ProductInfo> specification = productInfoService.selectProductInfoList(productInfo);
		return specification;
	}
	
	/**
     * 保存订单
	 */
	@Log(title = "保存订单", action = "wap端保存订单")
	@PostMapping("/saveOrder")
	@ResponseBody
	public Message saveOrder(Order order) {
		//新增订单
		int a = RandomUtils.randomCommon(0, 9, 5);
		String time = DateUtils.getDateStamp(); 
		Order or = new Order(1, 1, 3, null, null, ShiroUtils.getLoginName(), order.getTypeId(), order.getTypeName(), order.getProductId(), order.getFullName(), ShiroUtils.getUserId().intValue(), ShiroUtils.getLoginName(), 1);
		or.setOrderNum("azx"+time+""+a+"");
		or.setClueId(order.getClueId());
		or.setDeptId(ShiroUtils.getUser().getDeptId().intValue());
		or.setDeptName(ShiroUtils.getUser().getDept().getDeptName());
		orderService.saveOrder(or);
		CustomerClue clue = customerClueService.selectCustomerClueById(or.getClueId());
		clue.setClueOperationType(1);
		customerClueService.saveCustomerClue(clue);
	try {
		//成单时开启成单审批工作流
		String businessId = UUID.randomUUID().toString();
		//flowId 1:成单审批 2：退单审批
		WorkFlowApply workFlowApply = new WorkFlowApply(ShiroUtils.getUserId().intValue(), businessId, 1, "成单审批", or.getOrderId());
		
		if (workFlowApplyService.saveWorkFlowApply(workFlowApply) > 0) {
			// 只有在新增情况下才能开启流程
			if (StringUtils.isNull(workFlowApply.getApplyId())) {
				//开启成单审批流程
				commonService.startProcess(businessId,workFlowApply);
				
				//新增订单修改记录
				OrderModifyRecord orderModifyRecord = new OrderModifyRecord(or.getOrderId(), ShiroUtils.getUserId().intValue(), 3, "成单审核");
				orderModifyRecordService.insertOrderModifyRecord(orderModifyRecord);
			
				return Message.ok();
			}

		}
		
	} catch (Exception e) {
		return Message.error();
	}
	return Message.error();
	}
}
